/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is Forte for Java, Community Edition. The Initial * Developer of the Original Code is Sun Microsystems, Inc. Portions * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved. */ package org.netbeans.core; import java.util.Arrays; import java.util.Vector; import java.util.Collections; import java.util.Comparator; import java.awt.*; import javax.swing.text.Keymap; import javax.swing.*; import org.openide.DialogDescriptor; import org.openide.TopManager; import org.openide.util.actions.SystemAction; import org.openide.util.Utilities; /** A panel used in SHortcutsEditor for editing shortcuts (list of shortcuts). * * @author Ian Formanek */ public class ShortcutsPanel extends javax.swing.JPanel { private Keymap map; /** * @associates KeyStroke */ private Vector strokes; private ShortcutsEditor shortcutsEditor; private ActionsPanel addActionsPanel; private ActionsPanel editActionsPanel; /** Creates new form ActionsPanel */ public ShortcutsPanel(ShortcutsEditor shortcutsEditor) { this.shortcutsEditor = shortcutsEditor; updateData (); initComponents (); setBorder (new javax.swing.border.CompoundBorder( new javax.swing.border.TitledBorder( new javax.swing.border.EtchedBorder(), org.openide.util.NbBundle.getBundle(ShortcutsPanel.class).getString("ShortcutsPanel.ShortcutsTitle")), new javax.swing.border.EmptyBorder(new java.awt.Insets(8, 8, 8, 8)))); shortcutsList.setCellRenderer (new KeyStrokeRenderer ()); shortcutsList.setSelectedIndices(new int[0]); updateButtons (); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the FormEditor. */ private void initComponents () {//GEN-BEGIN:initComponents buttonsPanel = new javax.swing.JPanel (); addButton = new javax.swing.JButton (); removeButton = new javax.swing.JButton (); shortcutsScrollPane = new javax.swing.JScrollPane (); shortcutsList = new javax.swing.JList (strokes); setLayout (new java.awt.BorderLayout (0, 12)); buttonsPanel.setLayout (new java.awt.GridBagLayout ()); java.awt.GridBagConstraints gridBagConstraints1; addButton.setText (org.openide.util.NbBundle.getBundle(ShortcutsPanel.class).getString("ShortcutsPanel.addButton.text")); addButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { addButtonActionPerformed (evt); } } ); gridBagConstraints1 = new java.awt.GridBagConstraints (); gridBagConstraints1.insets = new java.awt.Insets (0, 0, 0, 8); buttonsPanel.add (addButton, gridBagConstraints1); removeButton.setText (org.openide.util.NbBundle.getBundle(ShortcutsPanel.class).getString("ShortcutsPanel.removeButton.text")); removeButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { removeButtonActionPerformed (evt); } } ); gridBagConstraints1 = new java.awt.GridBagConstraints (); gridBagConstraints1.insets = new java.awt.Insets (0, 0, 0, 8); gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints1.weightx = 1.0; buttonsPanel.add (removeButton, gridBagConstraints1); add (buttonsPanel, java.awt.BorderLayout.SOUTH); shortcutsList.addListSelectionListener (new javax.swing.event.ListSelectionListener () { public void valueChanged (javax.swing.event.ListSelectionEvent evt) { shortcutsListValueChanged (evt); } } ); shortcutsScrollPane.setViewportView (shortcutsList); add (shortcutsScrollPane, java.awt.BorderLayout.CENTER); }//GEN-END:initComponents private void addButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed if (addActionsPanel == null) { addActionsPanel = new ActionsPanel (true, shortcutsEditor); } DialogDescriptor dd = new DialogDescriptor (addActionsPanel, org.openide.util.NbBundle.getBundle(ShortcutsPanel.class).getString("ShortcutsPanel.AddActionTitle")); java.awt.Dialog d = TopManager.getDefault().createDialog(dd); d.show (); if (dd.getValue() == DialogDescriptor.OK_OPTION) { updateData (); shortcutsList.setListData(strokes); shortcutsEditor.setModified (true); } }//GEN-LAST:event_addButtonActionPerformed private void shortcutsListValueChanged (javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_shortcutsListValueChanged updateButtons (); }//GEN-LAST:event_shortcutsListValueChanged private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed int[] selectedIndices = shortcutsList.getSelectedIndices(); Object[] selected = shortcutsList.getSelectedValues(); for (int i = 0; i < selected.length; i++) { map.removeKeyStrokeBinding((KeyStroke)selected[i]); strokes.remove (selected[i]); } if (selectedIndices.length == 1) { shortcutsList.setSelectedIndex (selectedIndices[0]); } else { shortcutsList.setSelectedIndices (new int[0]); } shortcutsList.revalidate (); shortcutsList.repaint (); shortcutsEditor.setModified (true); }//GEN-LAST:event_removeButtonActionPerformed private void updateData () { map = TopManager.getDefault ().getGlobalKeymap (); KeyStroke[] strokesArray = map.getBoundKeyStrokes(); strokes = new Vector (); for (int i = 0; i < strokesArray.length; i++) { strokes.addElement (strokesArray[i]); } Collections.sort (strokes, new Comparator () { public int compare (Object o1, Object o2) { int k1 = ((KeyStroke)o1).getKeyCode (); int k2 = ((KeyStroke)o2).getKeyCode (); if (k1 < k2) return -1; else if (k1 > k2) return 1; else { int m1 = ((KeyStroke)o1).getModifiers (); int m2 = ((KeyStroke)o2).getModifiers (); if (m1 < m2) return -1; else if (m1 > m2) return 1; else return 0; } } } ); } private void updateButtons () { removeButton.setEnabled(shortcutsList.getSelectedIndices().length > 0); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel buttonsPanel; private javax.swing.JButton addButton; private javax.swing.JButton removeButton; private javax.swing.JScrollPane shortcutsScrollPane; private javax.swing.JList shortcutsList; // End of variables declaration//GEN-END:variables class KeyStrokeRenderer extends DefaultListCellRenderer { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent (list, value, index, isSelected, cellHasFocus); setText (" " + ShortcutsEditor.getKeyStrokeName ((KeyStroke)value)); // the leading space provides empty border on the left side of the list // NOI18N return this; } } } /* * Log * 9 Gandalf 1.8 1/13/00 Ian Formanek I18N * 8 Gandalf 1.7 1/9/00 Ian Formanek Improved loading and * adding shortcuts * 7 Gandalf 1.6 1/6/00 Ian Formanek Removed Edit button, add * works * 6 Gandalf 1.5 1/4/00 Ian Formanek * 5 Gandalf 1.4 12/1/99 Ian Formanek * 4 Gandalf 1.3 11/29/99 Ian Formanek * 3 Gandalf 1.2 11/26/99 Patrik Knakal * 2 Gandalf 1.1 11/25/99 Ian Formanek * 1 Gandalf 1.0 11/25/99 Ian Formanek * $ */